/*
 * @(#)xin.tianhui.cloud.weblog.core.thymeleaf.util
 * @(#)ThymeleafUtils.java	2018-04-09
 * 
 * Copyright © 2018-2027, All rights reserved.
 * homeanter
 */
package xin.tianhui.cloud.weblog.thymeleaf.util;

import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.model.IAttribute;
import org.thymeleaf.model.IProcessableElementTag;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.standard.expression.IStandardExpressionParser;
import org.thymeleaf.standard.expression.StandardExpressions;
import org.thymeleaf.util.EscapedAttributeUtils;
import static org.thymeleaf.util.StringUtils.trim;
import static org.thymeleaf.util.StringUtils.isEmptyOrWhitespace;
import static org.thymeleaf.util.Validate.notNull;

/**
 * <P>Description:Thymeleaf工具类 </P>
 * @ClassName: ThymeleafUtils
 * @author junchen homeanter@163.com  
 * @date 2018-04-09 19:48:12
 * @see TODO
 */
public class ThymeleafUtils {
	/**
	 * 解析属性中的表达式
	 * @param context
	 * @param attribute
	 * @return
	 * @author junchen homeanter@163.com
	 * @date 2018-04-09 19:53:31
	 */
	public static Object evaluateExpression(ITemplateContext context, IAttribute attribute){
		notNull(context, "context must not be null");
		notNull(attribute, "attribute must not be null");
		String expression = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), trim(attribute.getValue()));
		if(isEmptyOrWhitespace(expression)) {
			return null;
		}
		IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
		IStandardExpression evaluableExpression = expressionParser.parseExpression(context, expression);
		return evaluableExpression.execute(context);
	}
	
	/**
	 * 解析属性中的表达式
	 * @param context
	 * @param attribute
	 * @return Integer
	 * @author junchen homeanter@163.com
	 * @Data 2018-04-09 20:01:33
	 */
	public static Integer evaluateExpressionAsInteger(ITemplateContext context, IAttribute attribute){
		if(attribute != null) {
			Object value = evaluateExpression(context,attribute);
			if(value!=null) {
				try {
					return Integer.parseInt(value.toString());
				} catch (Exception e) {
					return null;
				}
			}
		}
		return null;
	}
	
	/**
	 * 解析属性中的表达式
	 * @param context
	 * @param attribute
	 * @return Integer
	 * @author junchen homeanter@163.com
	 * @Data 2018-04-09 20:01:33
	 */
	public static String evaluateExpressionAsStr(ITemplateContext context, IAttribute attribute){
		if(attribute != null) {
			Object value = evaluateExpression(context,attribute);
			if(value!=null) {
				return value.toString();
			}
		}
		return null;
	}
	
	/**
	 * 解析属性中的表达式
	 * @param context
	 * @param attribute
	 * @return Integer
	 * @author junchen homeanter@163.com
	 * @Data 2018-04-09 20:01:33
	 */
	public static Boolean evaluateExpressionAsBoolean(ITemplateContext context, IAttribute attribute){
		if(attribute != null) {
			Object value = evaluateExpression(context,attribute);
			if(value!=null) {
				return Boolean.getBoolean(value.toString());
			}
		}
		return false;
	}
	
	public static Integer getValueAsInteger(ITemplateContext context,IProcessableElementTag elementTag,String attrName) {
		IAttribute attribute= elementTag.getAttribute(attrName);
		if(attribute ==null) {
			return null;
		}
		return evaluateExpressionAsInteger(context, attribute);
	}
	
	public static String getValueAsStr(ITemplateContext context,IProcessableElementTag elementTag,String attrName) {
		IAttribute attribute= elementTag.getAttribute(attrName);
		if(attribute ==null) {
			return null;
		}
		return evaluateExpressionAsStr(context, attribute);
	}
	
	public static Boolean getValueAsBoolean(ITemplateContext context,IProcessableElementTag elementTag,String attrName) {
		IAttribute attribute= elementTag.getAttribute(attrName);
		if(attribute ==null) {
			return null;
		}
		return evaluateExpressionAsBoolean(context, attribute);
	}
}
